<?php
/**
 * 
 *
 * @author Chen HuiYun <11chenhuiyun@gmail.com>
 * @copyright Copyright &copy; 2012-2020 MyyTaijii Software LLC
 */

/**
 * @author Chen HuiYun
 */
class BHttpRequest
{
	private $_request = array();
	
	public function __construct()
	{
		$this->normalizeRequest();
		
		if(isset($_REQUEST))
			$this->_request = $_REQUEST;
		else 
			$this->_request = array();
	}
	
	protected function normalizeRequest()
	{
		// normalize request
		if(function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc())
		{
			if(isset($_GET))
				$_GET=$this->stripSlashes($_GET);
			if(isset($_POST))
				$_POST=$this->stripSlashes($_POST);
			if(isset($_REQUEST))
				$_REQUEST=$this->stripSlashes($_REQUEST);
			if(isset($_COOKIE))
				$_COOKIE=$this->stripSlashes($_COOKIE);
		}
	}

	/**
	 * Strips slashes from input data.
	 * This method is applied when magic quotes is enabled.
	 * @param mixed $data input data to be processed
	 * @return mixed processed data
	 */
	public function stripSlashes(&$data)
	{
		return is_array($data)?array_map(array($this,'stripSlashes'),$data):stripslashes($data);
	}
	
	public function setRequestValue($key, $value)
	{
		$request = $this->_request;
		
		if (isset($request[$key])) {
			$request[$key] = $value;
		}
		else {
			$request = array_merge($request, array($key, $value));
		}
		
		$this->_request = $request;
	}
	
	public function getRequestValue($key)
	{
		$request = $this->_request;
		return isset($request[$key]) ? $request[$key] : null;
	}
}